package com.nbjtjc.safe.api.emg.group;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgGroupMemberMapper;
import com.nbjtjc.safe.model.emg.EmgGroupMember;
import com.nbjtjc.safe.service.emg.EmgGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/groupMember")
@CrossOrigin("*")
public class GroupMemberController {
    @Autowired
    private EmgGroupMemberMapper emgGroupMemberMapper;
    @Autowired
    private EmgGroupMemberService emgGroupMemberService;

    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @SysLog("删除应急队伍里面的人员")
    public Result delete(EmgGroupMember emgGroupMember) throws Exception {
        Result result = new Result();
        try {
            result = emgGroupMemberService.delete(emgGroupMember);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    @SysLog("添加应急队伍里面的人员")
    public Result add(EmgGroupMember emgGroupMember) {
        Result result = new Result();
        try {
            result = emgGroupMemberService.add(emgGroupMember);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(EmgGroupMember emgGroupMember) {
        Result result = new Result();
        if (emgGroupMember == null || emgGroupMember.getGroupId() == null) {
            result.setFalse("groupId不能为空");
            return result;
        }
        try {
            List<EmgGroupMember> emgGroupMembers = emgGroupMemberMapper.findByGroupId(emgGroupMember.getGroupId());
            result.setData(emgGroupMembers);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
